﻿Imports System.Data.SqlClient
Imports System.Text.RegularExpressions
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub


    Private Sub 關於AToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 關於AToolStripMenuItem.Click
        My.Forms.AboutBox1.Show()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If JSystem.IsValidEmail(Me.TextBox1.Text) = True Then
            Me.ListBox1.Items.Add(Me.TextBox1.Text.Trim)
            Me.ListBox1.Sorted = True
            Me.TextBox1.Clear()
        Else
            MsgBox(("不是正確的Email" & vbCrLf & "請重新輸入"))
        End If

    End Sub

    Private Sub 貼上PToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ClipStrig As String
        ClipStrig = Clipboard.GetText.Trim


    End Sub



    Private Sub 匯入ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 匯入ToolStripMenuItem.Click
        Dim selectfile As OpenFileDialog
        selectfile = New OpenFileDialog
        selectfile.Multiselect = False
        selectfile.Filter = "文字檔(txt)|*.txt|CSV檔(CSV)|*.CSV|文字檔(text)|*.text|全部檔案|*.*"
        selectfile.ShowDialog()
        If selectfile.FileName.Length > 0 Then
            Dim fileContents As String
            fileContents = My.Computer.FileSystem.ReadAllText(selectfile.FileName)
            Dim SplitFileContents = Regex.Matches(fileContents, "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")


            '    fileContents = JSystem.CleanStr(fileContents)
            '    Dim SplitFileContents = fileContents.Split(vbCrLf.ToCharArray)

            Dim index_list As Integer
            index_list = SplitFileContents.Count

            If index_list > 0 Then
                For index As Integer = 0 To index_list - 1
                    If (SplitFileContents.Item(index).ToString.Trim.Length > 0) And (JSystem.IsValidEmail(SplitFileContents.Item(index).ToString.Trim.ToString) = True) Then
                        ListBox1.Items.Add(SplitFileContents.Item(index).ToString.Trim)
                    End If

                Next
            End If
            Me.ListBox1.Sorted = True
            Me.Label_listcount.Text = "列表數量：" & Me.ListBox1.Items.Count.ToString & "筆"

        End If

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.ListBox1.Sorted = True


        Dim DelCount As Integer
        DelCount = 0

        For index As Integer = 0 To Me.ListBox1.Items.Count - 1
            If index = ListBox1.Items.Count - 1 Then
            Else
                If (ListBox1.Items(index) <> "ZZZZ_WaitForDel") And (ListBox1.Items(index) = ListBox1.Items(index + 1)) Then
                    ListBox1.Items.RemoveAt(index + 1)
                    ListBox1.Items.Add("ZZZZ_WaitForDel")
                    DelCount = DelCount + 1
                End If
            End If
        Next



        Do While Me.ListBox1.FindStringExact("ZZZZ_WaitForDel") >= 0
            ListBox1.Items.RemoveAt(Me.ListBox1.FindStringExact("ZZZZ_WaitForDel"))
        Loop

        Me.Label_listcount.Text = "列表數量：" & Me.ListBox1.Items.Count.ToString & "筆"
        Me.Label_delcount.Visible = True
        Me.Label_delcount.Text = "已經刪除：" & DelCount.ToString & "筆"




    End Sub

    Private Sub 匯入文本ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 匯入文本ToolStripMenuItem.Click
        Dim selectfile As OpenFileDialog
        selectfile = New OpenFileDialog
        selectfile.Multiselect = False

        selectfile.Filter = "HTML|*.html|HTM|*.htm|文字檔(txt)|*.txt|文字檔(text)|*.text"
        selectfile.ShowDialog()

        If selectfile.FileName.Length > 0 Then
            Dim fileContents As String
            fileContents = My.Computer.FileSystem.ReadAllText(selectfile.FileName)
            Me.TextBox_body.Text = fileContents
        End If

    End Sub

    Private Sub 從網頁匯入文本ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 從網頁匯入文本ToolStripMenuItem.Click

        My.Forms.openurl.Show()

    End Sub

    Private Sub 寄發ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 寄發ToolStripMenuItem.Click

        For index As Integer = 0 To Me.ListBox1.Items.Count - 1
            JSystem.insert_mail_threading(My.User.Name, 0, Me.TextBox_sender.Text, Me.TextBox_sender.Text, Me.ListBox1.Items(index).ToString.Trim.Replace(",", "."), "", "", Me.TextBox_sender.Text, "True", Me.TextBox_subject.Text, "Encoding.UTF8", Me.TextBox_body.Text, "Encoding.UTF8")

        Next


    End Sub
End Class
